package com.example.costumview.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class NormalGestureTack2View extends View {

    private final Paint mPaint;
    private Path mPath;

    public NormalGestureTack2View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(8);

        mPath=new Path();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(mPath,mPaint);
    }

    private float mPreX,mPreY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN){
            mPreX=event.getX();
            mPreY=event.getY();
            mPath.moveTo(mPreX,mPreY);
//            invalidate();
            return true;
        }else if (event.getAction()==MotionEvent.ACTION_MOVE){

            float endX=(mPreX+event.getX())/2;
            float endY=(mPreY+event.getY())/2;
            mPath.quadTo(mPreX,mPreY,endX,endY);
            mPreX=event.getX();
            mPreY=event.getY();
            invalidate();
        }
        return super.onTouchEvent(event);
    }
}
